import React, { PureComponent } from 'react';
import { connect } from 'dva';
import { FormattedMessage } from 'umi/locale';
import { Form, Button, Card, Input, notification } from 'antd';
import PageHeaderWrapper from '@/components/PageHeaderWrapper';

const FormItem = Form.Item;

@connect(({ customerServicePhone, loading }) => ({
  customerServicePhone,
  loading: loading.models.list,
}))
@Form.create()
class ServiceTel extends PureComponent {
  componentDidMount() {
    const { dispatch } = this.props;
    dispatch({
      type: 'customerServicePhone/fetch',
    });
  }

  handleSubmit = e => {
    const { dispatch, form } = this.props;
    e.preventDefault();
    form.validateFieldsAndScroll(async (err, values) => {
      if (!err) {
        await dispatch({
          type: 'customerServicePhone/update',
          payload: values,
        });
        notification.success({
          message: '成功提交',
          description: '您已成功提交！',
        });
      }
    });
  };

  render() {
    const {
      customerServicePhone: { customerServicePhone },
      submitting,
    } = this.props;
    const {
      form: { getFieldDecorator },
    } = this.props;

    const submitFormLayout = {
      wrapperCol: {
        xs: { span: 24, offset: 0 },
        sm: { span: 10, offset: 7 },
      },
    };

    return (
      <PageHeaderWrapper title="客服电话" content="客服电话">
        <Card bordered={false}>
          <Form onSubmit={this.handleSubmit} hideRequiredMark style={{ marginTop: 8 }}>
            {customerServicePhone ? (
              <FormItem label="客服电话" {...this.formLayout}>
                {getFieldDecorator('integer', {
                  rules: [{ required: true, message: '请输入正确的电话', max: 11 }],
                  initialValue: customerServicePhone.integer,
                })(<Input placeholder="请输入客服电话" />)}
              </FormItem>
            ) : (
              ''
            )}
            <FormItem {...submitFormLayout} style={{ marginTop: 32 }}>
              <Button type="primary" htmlType="submit" loading={submitting}>
                <FormattedMessage id="form.submit.label" />
              </Button>
            </FormItem>
          </Form>
        </Card>
      </PageHeaderWrapper>
    );
  }
}

export default ServiceTel;
